package daoimpl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import pojo.Price;
import pojo.UserProfile;
import dao.PriceDAO;

@Repository("PriceDAO")
public class PriceDAOImpl implements PriceDAO {
	
	private SessionFactory sessionFactory;
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public List<Price> getListPrice() {
		List<Price> ds = null;
		
		
		Session session = null;
		try{
			session = sessionFactory.getCurrentSession();
		}
		catch(HibernateException ex)
		{
			session = sessionFactory.openSession();
		}
			
        try {
            String hql = "Select price from Price price";
            Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
        	session.close();
        }
        return ds;
	}

	public Price getPriceByID(int id) {
		Price price = null;
		
		Session session = null;
		try{
			session = sessionFactory.getCurrentSession();
		}
		catch(HibernateException ex)
		{
			session = sessionFactory.openSession();
		}
			
        try {
            String hql = "Select price from Price price where price.idprice=:idprice";
            Query query = session.createQuery(hql);
            query.setParameter("idprice", id);
            price = (Price)query.uniqueResult();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
        	session.close();
        }
        return price;
	}

}
